package com.vlong.town.controller.admin.townInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.townInfo.TownInfo;
import com.vlong.town.service.townInfo.TownInfoService;
import com.vlong.town.util.common.TownConstant;

/**
 * 村镇信息Controller
 * @author Assassin
 */
@Controller
@RequestMapping("/town")
public class TownInfoController extends BaseController {

    // Fields

    @Autowired
    private TownInfoService townInfoService;// 村镇信息业务实现类

    /**
     * 进入村镇信息维护页面
     * @return townInfo/townInfo
     */
    @GetMapping("/info")
    public String gotoTownInfoPage() {
        Long townId = 1L;
        TownInfo townInfo = this.townInfoService.query(townId);
        request.setAttribute("townInfo", townInfo);
        return "admin/townInfo/townInfo";
    }

    /**
     * 保存村镇信息
     * 这里的参数需要根据实际情况自行修改
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/save")
    public DataMessage saveTownInfo(
            @RequestParam(required=true, value="townId") Long townId,
            @RequestParam(required=true, value="title") String title,
            @RequestParam(required=true, value="content") String content) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(townId == null) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("村镇标识不合法");
            return dm;
        }
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("介绍标题不合法");
            return dm;
        }
        if(content == null || content.trim().length() == 0) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("介绍内容不合法");
            return dm;
        }
        TownInfo townInfo = new TownInfo();
        townInfo.setTownId(townId);
        townInfo.setTitle(title);
        townInfo.setContent(content);
        this.townInfoService.update(townInfo);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("村镇介绍保存成功");
        return dm;
    }

}
